<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />





  <link rel="alternate" href="/atom.xml" title="renhao" type="application/atom+xml" />






<meta name="description" content="盒模型中对一个元素设置css需要元素是block类型还是inline类型。BFC就是用来格式化块级（block）盒子，还有管理inline类型的盒子IFC，以及其他的FC。">
<meta property="og:type" content="article">
<meta property="og:title" content="BFC">
<meta property="og:url" content="https://huangrenhao.gitee.io/2019/09/27/css-bfc/index.html">
<meta property="og:site_name" content="renhao">
<meta property="og:description" content="盒模型中对一个元素设置css需要元素是block类型还是inline类型。BFC就是用来格式化块级（block）盒子，还有管理inline类型的盒子IFC，以及其他的FC。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-09-03T08:50:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="BFC">
<meta name="twitter:description" content="盒模型中对一个元素设置css需要元素是block类型还是inline类型。BFC就是用来格式化块级（block）盒子，还有管理inline类型的盒子IFC，以及其他的FC。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://huangrenhao.gitee.io/2019/09/27/css-bfc/"/>





  <title>BFC | renhao</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">renhao</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangrenhao.gitee.io/2019/09/27/css-bfc/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="renhao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="renhao">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">BFC</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-27T07:38:54+08:00">
                2019-09-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/css/" itemprop="url" rel="index">
                    <span itemprop="name">css</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/09/27/css-bfc/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/09/27/css-bfc/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.1k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  4
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>盒模型中对一个元素设置css需要元素是block类型还是inline类型。BFC就是用来格式化块级（block）盒子，还有管理inline类型的盒子IFC，以及其他的FC。<a id="more"></a>FC（Formatting Context)指页面中的一个渲染区域并且拥有一套渲染规则（决定了其他子元素如何定位以及与其他元素的相互关系和作用）。BFC即块级格式化上下文，指一个独立的块级渲染区域，只有Block-level Box参与，该区域拥有一套渲染规则来约束块级盒子的布局且与区域外部无关。</p>
<h3 id="BFC的生成"><a href="#BFC的生成" class="headerlink" title="BFC的生成"></a>BFC的生成</h3><p>css2.1中规定满足下列css声明之一的元素便会生成BFC：</p>
<ul>
<li>float的值不为none</li>
<li>overflow的值不为visible</li>
<li>display的值为inline-block、table-cell、table-caption（display为table也可以生成，但在于Table会默认生成一个匿名的table-cell）</li>
<li>position的值为absolute或fixed</li>
</ul>
<h3 id="BFC的约束规则"><a href="#BFC的约束规则" class="headerlink" title="BFC的约束规则"></a>BFC的约束规则</h3><p>浏览器对BFC区域的约束规则：</p>
<ul>
<li>生成BFC元素的子元素会一个接一个的放置，垂直方向上它们的起点是一个包含块的顶部，两个相邻子元素之间垂直距离取决于元素margin特性。在BFC中相邻的块级元素外边距会折叠。</li>
<li>生成BFC元素的子元素中，每一个子元素的外边距和包含块的左边界相接触（对于从右到左的格式化右外边距和右边界相接触），除非这个子元素也创建了一个新的BFC（如它自身也是一个浮动元素）。</li>
</ul>
<blockquote>
<p>详细说明：</p>
<ul>
<li>内部的Box会在垂直方向上一个接一个的放置</li>
<li>垂直方向上的距离由margin决定（属于同一个BFC的两个相邻Box的margin会发生重叠，与方向无关）</li>
<li>每个元素的左外边距与包含块的左边界相接触（从左到右），即使浮动元素也是如此（说明BFC中的子元素不会超过它的包含块，而position为absolute的元素可以超过它的包含块边界）</li>
<li>BFC的区域不会与float的元素区域重叠</li>
<li>计算BFC的高度时，浮动子元素也参与计算。</li>
<li>BFC就是页面上的一个隔离的独立容器，容器里面的子元素不会影响到外面的元素，反之亦然</li>
</ul>
<p>类似css规则：</p>
<ul>
<li>Block元素会扩展到与父元素同宽，所有block元素会垂直排列</li>
<li>垂直方向上两个相邻div的margin会重叠，而水平方向可能不会</li>
<li>浮动元素会尽量往左上方（或右下方）</li>
<li>为父元素设置overflow：hidden或浮动父元素，则会包含浮动元素</li>
</ul>
</blockquote>
<h3 id="BFC在布局中的应用"><a href="#BFC在布局中的应用" class="headerlink" title="BFC在布局中的应用"></a>BFC在布局中的应用</h3><ul>
<li><p>防止margin相互重叠</p>
<p>同一个BFC中两个相邻的Box才会发生重叠，与方向无关。阻止margin重叠只要将两个元素别放在一个BFC中即可（可以让相邻元素其中一个生成BFC），我们甚少看到水平方向的margin重叠，但可以设置write-mode（文本水平或垂直排布以及块级元素中文本的行进方向）使其水平排列的时候可能会出现margin水平方向重叠</p>
<p><code>`</code><br>&lt;!DOCTYPE html&gt;<br><html><br><head><meta name="generator" content="Hexo 3.8.0"><br>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head></html></p>
  <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
    on iOS devices-->
<p>  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"></p>
  <title></title>

  <style> 
    html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
    #map{
      padding:0;
    }
    .first{
      margin:20px;
      background:lightgreen;
      width:100px;
      height:100px;
    }
    ul{
      /*display:inline-block;*/
      margin:10px;
      background:lightblue;
    }
    li{
      margin:25px;
    }
  </style> 


</li>
</ul>
<p>   </p>
  <body class="claro"><br>    <div class="first"></div><br>    <ul><br>      <li>1</li><br>      <li>2</li><br>      <li>3</li><br>    </ul><br>  </body> 

<p>  </p>
<pre><code>


- 浮动相关问题

  为了使父元素包含子浮动元素，常见的方式是父元素设置overflow：hidden或浮动父元素。**根本原因在于将父元素变为BFC，子浮动元素就会参与其高度计算**（即不会产生高度塌陷问题），overflow：hidden与正常流最接近。

</code></pre><p>  &lt;!DOCTYPE html&gt;<br>  <html><br>  <head><meta name="generator" content="Hexo 3.8.0"><br>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><br>    <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
      on iOS devices--><br>    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"><br>    <title></title></head></html></p>
<pre><code>&lt;style&gt; 
  html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
  #map{
    padding:0;
  }
  .first{
    margin:20px;
    background:lightgreen;
    border: 2px solid lightgreen;
    /*display:inline-block;*/
    /*overflow:hidden;*/
    /*float: left;*/
    /*position: absolute;*/
  }
  ul{
    overflow:hidden;
    margin:10px;
    background:lightblue;
    width:100px;
    height:200px;
    float: left;
  }
  li{
    margin:25px;
  }
&lt;/style&gt; 
</code></pre><p>   </p>
  <body class="claro"><br>    <div class="first"><br>      <ul><br>        <li>1</li><br>        <li>2</li><br>        <li>3</li><br>      </ul><br>    </div><br><br>  </body> 

<p>  </p>
<pre><code>


- 多栏布局的一种方式

  与浮动元素相邻的已生成BFC元素不能与浮动元素相互覆盖，可以实现多栏布局

</code></pre><p>  &lt;!DOCTYPE html&gt;<br>  <html><br>  <head><meta name="generator" content="Hexo 3.8.0"><br>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><br>    <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
      on iOS devices--><br>    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"><br>    <title></title></head></html></p>
<pre><code>&lt;style&gt; 
  html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
  .left{
    background:pink;
    float: left;
    width:180px;
  }
  .center{
    background:lightyellow;
    overflow:hidden;

  }
  .right{
    background: lightblue;
    width:180px;
    float:right;
  }
&lt;/style&gt; 
</code></pre><p>   </p>
<p>  <body class="claro"><br>    <div class="container"><br>      <div class="left"><br>        <pre><br>    .left{<br>      background:pink;<br>      float: left;<br>      width:180px;<br>    }<br>        </pre><br>      </div><br>      <div class="right"><br>         <pre><br>    .right{<br>      background:lightblue;<br>      width:180px;<br>      float:right;<br>    }<br>        </pre><br>      </div><br>      <div class="center"><br>      <pre><br>    .center{<br>      background:lightyellow;<br>      overflow:hidden;<br>      height:116px;<br>    }<br>        </pre><br>      </div><br>    </div></body></p>
<p>  <br>  <code>`</code></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/09/26/css-flex-justify-content/" rel="next" title="CSS flex布局两端对齐最后一行列表左对齐的方法">
                <i class="fa fa-chevron-left"></i> CSS flex布局两端对齐最后一行列表左对齐的方法
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/09/27/css-haslayout/" rel="prev" title="hasLayout">
                hasLayout <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">renhao</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">94</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#BFC的生成"><span class="nav-number">1.</span> <span class="nav-text">BFC的生成</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#BFC的约束规则"><span class="nav-number">2.</span> <span class="nav-text">BFC的约束规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#BFC在布局中的应用"><span class="nav-number">3.</span> <span class="nav-text">BFC在布局中的应用</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">renhao</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">265.9k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '2P1xqUWiCoNm14MH4yhURlJi-gzGzoHsz',
        appKey: 'uJN0uagHIVgXtBO1OuLV9Ban',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
